<!--#include virtual="header.shtml" -->

<CENTER><H3><FONT COLOR="#AOAO99">
CCmdLine - a class for parsing command line with or without MFC
</FONT></H3></CENTER><HR>

<!-- Author and contact details -->
This article was contributed by <A HREF="mailto:smallest@smalleranimals.com">Chris Losinger</A>.

<!-- Environment eg NT 4.0 SP3, VC6.0 SP1 -->
<p>Environment: All</u> 

<!-- Text / source code -->
<p>
<!-- The 'p' starts a paragraph of normal text -->

CCmdLine is a simple way to parse a command line into <i>switches</i> and <i>arguments</i>.
Ex :
<br>
<ul>
MyApp.exe -sw1 arg1 arg2 -sw2 arg3 -sw3 -sw4
</ul>
<br>
When using CCmdLine, "-sw1", "-sw2", "-sw3" and "-sw4" are <i>switches</i> and "arg1", "arg2" and "arg3" are <i>arguments</i>.
<p>
Parsing command lines with the standard C string functions (strlen, strcpy, etc.) or even 
with the CString operators is a nightmare. But, CCmdLine makes it effortless.
<p>
CCmdLine was written for use with console apps, but can be easily used in 
any application which requires command-line parsing.
<p>
CCmdLine uses STL for its collection classes, so it works in MFC 
and non-MFC apps. If you are using this in an MFC app, the switches 
and arguments will be returned as CStrings. If you are using this in a 
non-MFC app, they will be returned as STL 'string's.
<p>
<hr>
Example :

Assume the application we're writing uses a command line that has two
required switches and two optional switches. The app should abort
if the required switches are not present and continue with default
values if the optional switches are not present.
<ul>
Sample command line : 
<br>
<pre>
  MyApp.exe -p1 text1 text2 -p2 "this is a big argument" -opt1 -55 -opt2
</pre>
Switches -p1 and -p2 are required. 
<br>
p1 has two arguments and p2 has one.
<br>
<br>
Switches -opt1 and -opt2 are optional. 
<br>
opt1 requires a numeric argument. 
<br>
opt2 has no arguments.
<br>
<br>   
Also, assume that the app displays a 'help' screen if the '-h' switch
is present on the command line.
</ul>
Here's how you can use CCmdLine to handle this :
<hr>
<!-- start a block of source code -->
<PRE><TT><FONT COLOR="#990000">
// if this is an MFC app, uncomment this line
// #include "stdafx.h"

#include "CmdLine.h"

void main(int argc, char **argv)
{
  // our cmd line parser object
  CCmdLine cmdLine;

  // parse the command line
  // use __argc and __argv, in MFC apps
  if (cmdLine.SplitLine(argc, argv) < 1)
  {
     // no switches were given on the command line, abort
     ASSERT(0);
     exit(-1);
  }

  // test for the 'help' case
  if (cmdLine.HasSwitch("-h"))
  {
     show_help();
     exit(0);
  }

  // StringType is CString when using MFC, else STL's 'string'
  StringType p1_1, p1_2, p2_1;

  // get the required arguments
  try
  {  
     // if any of these GetArgument calls fail, 
     // we'll end up in the catch() block

     // get the first -p1 argument
     p1_1 = cmdLine.GetArgument("-p1", 0);

     // get the second -p1 argument
     p1_2 = cmdLine.GetArgument("-p1", 1);

     // get the first -p2 argument
     p2_1 = cmdLine.GetArgument("-p2", 0);

  }
  catch (...)
  {
     // one of the required arguments was missing, abort
     ASSERT(0);
     exit(-1);
  }

  // get the optional parameters

  // GetSafeArgument does not throw exceptions, and allows for 
  // the use of a default value, in case the switch is not found
  // convert to an int, default to '100'
  int iOpt1Val =    atoi( cmdLine.GetSafeArgument( "-opt1", 0, 100 ) );

  // since opt2 has no arguments, just test for the presence of
  // the '-opt2' switch
  bool bOptSwitch2 =   cmdLine.HasSwitch("-opt2");

  .... and so on....

}
<!-- end the block of source code -->
</FONT></TT></PRE>

<!-- demo project -->

<p><A HREF="cmdline.zip">Download source - 5 Kb</A>


<!-- Date Posted and Last update (eg August 11, 1998) -->
<p>Date Posted:  June 16, 1999

<!--#include virtual="footer.shtml" -->